function handleTree(data, id = 'id', pid = 'pid', children = 'children') {
	let config = {
		id: id || 'id',
		pid: pid || 'pid',
		childrenList: children || 'children'
	}

	var childrenListMap = {}
	var nodeIds = {}
	var tree = []

	for (let d of data) {
		let pid = d[config.pid]
		if (childrenListMap[pid] == null) {
			childrenListMap[pid] = []
		}
		nodeIds[d[config.id]] = d
		childrenListMap[pid].push(d)
	}

	for (let d of data) {
		let pid = d[config.pid]
		if (nodeIds[pid] == null) {
			tree.push(d)
		}
	}

	for (let t of tree) {
		adaptToChildrenList(t)
	}

	function adaptToChildrenList(o) {
		if (childrenListMap[o[config.id]] !== null) {
			o[config.childrenList] = childrenListMap[o[config.id]]
		}
		if (o[config.childrenList]) {
			for (let c of o[config.childrenList]) {
				adaptToChildrenList(c)
			}
		}
	}
	return tree
}
export { handleTree }
